<?php
	session_start();
	
	require_once('../../lib/'.'BaseDir.inc.htm');
	require_once (EIU_LIB.'View.class.htm');
	require_once('../../lib/'.'DBCommon.class.htm');
	
	$today = date("Y-m-d h:i:s");
	$modifiedby = "";
	if(isset($_SESSION['userName']) && $_SESSION['userName']!=""){
		$modifiedby = $_SESSION['userName'];
	}
	
	if(isset($_POST['isDeleted']) && $_POST['isDeleted']==1)
	{
		$oDB = new DBCommon();
		$conn =  $oDB->stl_openConn();
		//update isDeleted
		$q_del_supplier = "UPDATE `supplier_tbl` SET `isDeleted` = '1',
								`LastUpdate` = '".$today."',
								`ModifiedBy` = '".$modifiedby."'
								WHERE `supplier_tbl`.`SuppID` = '".$_POST['SuppliID']."'";
								
		$rs_delete = $oDB->stl_db_change($q_del_supplier, $conn);
		echo $rs_delete;
		
		$oDB->stl_closeConn($conn);
	}
	
	if(isset($_POST['info']) && !empty($_POST['info'])){
		$oDB = new DBCommon();
		$conn =  $oDB->stl_openConn();
		
		$_POST['info'] = str_replace('\"', '"',$_POST['info']);
		$customer = json_decode($_POST['info'],true);
	//	print_r($customer);
		if(is_array($customer)){
			if(empty($_POST['SuppliID']))
			{
				//select TaxCode,ShortName
				$q="SELECT `ComName`,`ShortName`,`TaxCode` FROM `supplier_tbl` WHERE `isDeleted`=0";
				$rs = $oDB->stl_db_select($q, $conn);
				if(is_array($rs))
				{
					foreach($rs as $key=>$value)
					{
						if(($customer['name']==$rs[$key]->ComName)&&($customer['shortname']==$rs[$key]->ShortName)&&($customer['taxcode']==$rs[$key]->TaxCode))
						{
							exit;
						}
					}
				}
				//insert supplier moi
				$q_insert_supplier = "INSERT INTO `supplier_tbl` (
					`ComName` ,`ShortName`,`ComAddress` ,`ComTel` ,`ComFax` ,`TaxCode` ,`BankName` ,`BankCN` ,
					`BankAcc` ,`ContactName` ,`ContactPosition` ,`ContactTel` ,`ContactMobile` ,`isDeleted` ,
					`CreateDate` ,`LastUpdate` ,`ModifiedBy`
					)
					VALUES (
					'".$customer['name']."','".$customer['shortname']."', '".$customer['address']."', '".$customer['tel']."', '".$customer['fax']."', '".$customer['taxcode']."', '".$customer['bankname']."', '".$customer['bankcn']."', '".$customer['bankacc']."', '".$customer['name1']."','".$customer['position']."','".$customer['tel1']."', '".$customer['mobile']."', '0', '".$today."', '".$today."', '".$modifiedby."'
					);";
				//print_r($q_insert_supplier);
				$rs_insert = $oDB->stl_db_change($q_insert_supplier, $conn);
				echo $rs_insert;
			}else
			{
				//update cus_list_tbl
				$q_update_customer = "UPDATE `supplier_tbl` SET `ComName` = '".$customer['name']."',
					`ComAddress` = '".$customer['address']."',
					`ShortName` = '".$customer['shortname']."',
					`ComTel` = '".$customer['tel']."',
					`ComFax` = '".$customer['fax']."',
					`TaxCode` = '".$customer['taxcode']."',
					`BankName` = '".$customer['bankname']."',
					`BankCN` = '".$customer['bankcn']."',
					`BankAcc` = '".$customer['bankacc']."',
					`ContactName` = '".$customer['name1']."',
					`ContactPosition` = '".$customer['position']."',
					`ContactTel` = '".$customer['tel1']."',
					`ModifiedBy` = '".$modifiedby."',
					`LastUpdate` = '".$today."',
					`ContactMobile` = '".$customer['mobile']."' WHERE `supplier_tbl`.`SuppID` = '".$customer['SuppliID']."'";
									
				//	print_r($q_update_customer);
				$rs_update = $oDB->stl_db_change($q_update_customer, $conn);
				echo $rs_update;
			}
		}
		$oDB->stl_closeConn($conn);
	}
?>
